package conf

import (
	"fmt"
	"gitee.com/legend9999/publisher/common"
	"github.com/pelletier/go-toml"
	"log"
	"path/filepath"
)

type Config struct {
	*toml.Tree
}

var (
	SysConf = &Config{Tree: newSysConfig()}
)

/**
 * 返回单例实例
 * @method newSysConfig
 */
func newSysConfig() *toml.Tree {
	realPath := filepath.Join(common.GetRunPath(), "conf/sys_config.toml")
	config, err := toml.LoadFile(realPath)
	if err != nil {
		log.Panicf("load sys config err:%s", err)
	}
	return config
}
func (c *Config) PostgresUrl() string {
	url := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable password=%s",
		c.Get("database.host"),
		c.Get("database.port"),
		c.Get("database.username"),
		c.Get("database.dbname"),
		c.Get("database.password"))
	return url
}
func (c *Config) PostgresMaxIdleConnNum() int {
	i := c.Get("database.maxidleconns").(int64)
	return int(i)
}
func (c *Config) PostgresMaxOpenConnNum() int {
	i := c.Get("database.maxopenconns").(int64)
	return int(i)
}
func (c *Config) GetSessionKey() string {
	sqliteTree := c.Get("session.key")
	return sqliteTree.(string)
}
func (c *Config) GetSessionExpires() int64 {
	sqliteTree := c.Get("session.expires")
	return sqliteTree.(int64)
}
func (c *Config) GetTokenExpires() int {
	sqliteTree := c.Get("session.token")
	return sqliteTree.(int)
}
func (c *Config) GetAppPort() string {
	sqliteTree := c.Get("app.port")
	return sqliteTree.(string)
}
